/**
 * Licensed to the Apache Software Foundation （ASF） under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * （the "License"）； you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 * <p>
 * https://www.q3z3.com
 * QQ : 939313737
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.platform.modules.sys.controller;

import com.platform.common.aspectj.SubmitRepeat;
import com.platform.common.web.controller.BaseController;
import com.platform.common.web.utils.Pager;
import com.platform.common.web.domain.AjaxResult;
import com.platform.modules.sys.domain.SysUser;
import com.platform.modules.sys.service.SysUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;


/**
 * <p>
 * 系统用户表 控制层
 * </p>
 */
@Controller
@RequestMapping("/sys/user")
public class SysUserController extends BaseController {

    @Resource
    private SysUserService sysUserService;

    /**
     * 去列表页面
     */
    @RequiresPermissions(value = {"sys:user:query"})
    @GetMapping(value = "/toList")
    public String toList() {
        return "sys/user/list";
    }

    /**
     * 列表数据(分页)
     */
    @RequiresPermissions(value = {"sys:user:query"})
    @PostMapping(value = "/query")
    @ResponseBody
    public AjaxResult query(SysUser sysUser) {
        startPage("username");
        Pager<SysUser> pager = sysUserService.queryPager(sysUser);
        return AjaxResult.success(pager);
    }

    /**
     * 新增页面
     */
    @RequiresPermissions(value = {"sys:user:add"})
    @GetMapping(value = "/toAdd")
    public String toAdd() {
        return "sys/user/add";
    }

    /**
     * 新增数据
     */
    @RequiresPermissions(value = {"sys:user:add"})
    @PostMapping(value = "/add")
    @ResponseBody
    @SubmitRepeat
    public AjaxResult add(SysUser sysUser) {
        if (sysUserService.uniqueUsername(sysUser) > 0) {
            return AjaxResult.fail("账号已存在");
        }
        sysUserService.addSysUser(sysUser);
        return AjaxResult.successMsg("新增成功");
    }

    /**
     * 修改页面
     */
    @RequiresPermissions(value = {"sys:user:edit"})
    @GetMapping(value = "/toEdit")
    public String toEdit(Model model, Long id) {
        model.addAttribute("entity", sysUserService.getById(id));
        return "sys/user/edit";
    }

    /**
     * 修改数据
     */
    @RequiresPermissions(value = {"sys:user:edit"})
    @PostMapping(value = "/edit")
    @ResponseBody
    public AjaxResult edit(SysUser sysUser) {
        if (sysUserService.uniqueUsername(sysUser) > 0) {
            return AjaxResult.fail("账号已存在");
        }
        sysUserService.editSysUser(sysUser);
        return AjaxResult.successMsg("修改成功");
    }

    /**
     * 修改状态
     */
    @RequiresPermissions(value = {"sys:user:edit"})
    @PostMapping(value = "/changeStatus")
    @ResponseBody
    public AjaxResult changeStatus(SysUser sysUser) {
        sysUserService.changeStatus(sysUser);
        return AjaxResult.successMsg("修改成功");
    }

    /**
     * 删除数据
     */
    @RequiresPermissions(value = {"sys:user:delete"})
    @GetMapping(value = "/delete")
    @ResponseBody
    public AjaxResult delete(SysUser sysUser) {
        sysUserService.deleteById(sysUser.getId());
        return AjaxResult.successMsg("删除成功");
    }

    /**
     * 重置密码
     */
    @RequiresPermissions(value = {"sys:user:edit"})
    @PostMapping(value = "/reset")
    @ResponseBody
    public AjaxResult reset(SysUser sysUser) {
        sysUserService.reset(sysUser);
        return AjaxResult.successMsg("密码重置成功");
    }

}

